<?php
namespace app\admin\controller;
use app\admin\tool\Parser;
use app\common\model\ArticleCate as ArticleCateModel;
use think\Loader;
use think\Request;

/**
 * 父分类的先不做 把功能做完在做 先做到能用
 * 这个也现在是手动编辑 等把前端的模板做完了 再来解决这个控制的问题
 * Class ArticleCate
 * @package app\admin\controller
 */
class ArticleCate extends Base{

    public function index(){

        $model = ArticleCateModel::where([])->order('add_time desc')->paginate(10);

        $this->data['title'] = '文章分类';
        $this->data['parser'] = new Parser();
        $this->data['model'] = $model;
        $this->data['column'] = $this->column();

        return view('',$this->data);
    }

    public function add(){

        $this->data['title'] = '添加分类';

        return view('',$this->data);
    }

    public function edit(Request $request){
        $id = $request->param('id');

        $model = ArticleCateModel::get($id);
        $this->data['title'] = '分类编辑';
        $this->data['model'] = $model;

        return view('',$this->data);
    }

    public function create(Request $request){
        $data = $request->post();

        $validate = Loader::validate('ArticleCate');
        if(!$validate->check($data)){
            $this->error($validate->getError());
        }

        $data['add_time'] = $data['update_time'] = time();
        $data['op_user']  = $this->admin->id;

        $model = new ArticleCateModel();
        $model->data($data);
        if($model->allowField(true)->save()){
            $this->success("新增成功");
        }

        $this->error("操作失败");
    }

    public function update(Request $request){
        $data = $request->post();
        if(empty($data['id'])){
            $this->error("操作失败");
        }

        $validate = Loader::validate('ArticleCate');
        if(!$validate->check($data)){
            $this->error($validate->getError());
        }
        $data['update_time'] = time();

        $model = new ArticleCateModel();
        if($model ->allowField(true)-> save($data,['id'=>$data['id']])){
            $this->success("更新成功");
        }

        $this->error("操作失败");
    }

    /**
     * 获取模板列表 模板应该是一个公共的模块，按说不能写在这里  关系应该是 本模块依赖模板模块 所以应该是调用模板模块的功能 但是模板模块不能返回来调用
     * 暂时模板模块还没有做完 就这样吧
     */
    public function template(){
        $template_root = ROOT_PATH.'template'.DS;
        $files = opendir($template_root);

        return view('',$this->data);
    }

    protected function column(){

        return [
            'title'           =>['name'=>'分类标题', 'fun'=>'text','options'=>[]],
            'cover'           =>['name'=>'缩略图',   'fun'=>'text','options'=>[]],
            'seo_title'       =>['name'=>'seo标题',  'fun'=>'text','options'=>[]],
            'seo_desc'        =>['name'=>'seo描述',  'fun'=>'text','options'=>[]],
            'seo_keywords'    =>['name'=>'seo关键词','fun'=>'text','options'=>[]],
            'index_template'  =>['name'=>'封面模板', 'fun'=>'text','options'=>[]],
            'lists_template'  =>['name'=>'列表模板', 'fun'=>'text','options'=>[]],
            'content_template'=>['name'=>'内容模板', 'fun'=>'text','options'=>[]],
            'add_time'        =>['name'=>'添加时间', 'fun'=>'datetime','options'=>['Y-m-d H:i']],
            'update_time'     =>['name'=>'更新时间', 'fun'=>'text','options'=>[]],
        ];
    }

}